package org.csu.petstore_manage.service;

import org.csu.petstore_manage.domain.Item;
import org.csu.petstore_manage.domain.LineItem;
import org.csu.petstore_manage.domain.Order;
import org.csu.petstore_manage.persistence.ItemMapper;
import org.csu.petstore_manage.persistence.LineItemMapper;
import org.csu.petstore_manage.persistence.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private LineItemMapper lineItemMapper;
    @Autowired
    private ItemMapper itemMapper;

    public List<Order> getOrders(){
        return orderMapper.getOrders();
    }

    public Order getOrder(int orderId){
        Order order = orderMapper.getOrder(orderId);
        order.setLineItems(lineItemMapper.getLineItemsByOrderId(orderId));

        for (int i = 0; i < order.getLineItems().size(); i++) {
            LineItem lineItem = order.getLineItems().get(i);
            Item item = itemMapper.getItem(lineItem.getItemId());
            item.setQuantity(itemMapper.getInventoryQuantity(lineItem.getItemId()));
            lineItem.setItem(item);
        }

        return order;
    }

    public void deleteOrder(int orderId){
        orderMapper.deleteOrder(orderId);
        orderMapper.deleteOrderStatus(orderId);
    }

    public void updateOrder(Order order){
        orderMapper.updateOrder(order);
        orderMapper.updateOrderStatus(order);
    }


}
